﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Studio77.GuessTheWordsForWP7.Entity;
using Studio77.GuessTheWordsForWP7.Util;
using System.IO.IsolatedStorage;

namespace Studio77.GuessTheWordsForWP7.Core
{
    /// <summary>
    /// 关卡及关卡信息管理器
    /// </summary>
    public class StageManager
    {
        public static StageManager Instance = new StageManager();

        public bool TurnMoka = true;

        /// <summary>
        /// 关卡数
        /// </summary>
        public const int StageCount = 181;

        private StageManager()
        {
        }


        //keys
        private const string STAGE_DATA_KEY = "stageDataKey";
        private const string LAST_STAGE_KEY = "lastStageKey";
        private const string LAST_SCORE = "lastScore";
        private const string LAMP_COUNT = "lampCount";
        private const string MARKET_COMMENT = "marketComment";
        private const string LAUNCH_COUNT = "launchCount";
        private const string NEW_USER_TASK_KEY = "newUserTaskKey";

        private const string LAST_FULLSCREEN_DATE_KEY = "LAST_FULLSCREEN_DATE";

        //android的key，wp没法使用到
        //private const string LAMP_COUNT_KEY = "lampCountKey";
        //private const string GOLD_COUNT_KEY = "goldCountKey";
        //private const string ISBUY_ALL_STAGES_KEY = "isbuyAllStagesKey";
        //private const string LAST_STAGE_COUNT_KEY = "lastStageCountKey";

        private string lastFullScreenDate = "XXXXXX";
        /// <summary>
        /// 上一次显示全屏广告的日期
        /// </summary>
        public string LastFullScreenDate
        {
            get
            {
                return lastFullScreenDate;
            }
            set
            {
                lastFullScreenDate = value;
                IsolatedStorageSettings.ApplicationSettings[LAST_FULLSCREEN_DATE_KEY] = lastFullScreenDate;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int lastStage;
        public int LastStage
        {
            get
            {
                return lastStage;
            }
            set
            {
                lastStage = value;
                IsolatedStorageSettings.ApplicationSettings[LAST_STAGE_KEY] = lastStage;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int lastScore = 0;
        public int LastScore
        {
            get
            {
                return lastScore;
            }
            set
            {
                lastScore = value;
                IsolatedStorageSettings.ApplicationSettings[LAST_SCORE] = lastScore;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isDoNewUserTask = false;
        public bool IsDoNewUserTask
        {
            get
            {
                return isDoNewUserTask;
            }
            set
            {
                isDoNewUserTask = value;
                IsolatedStorageSettings.ApplicationSettings[NEW_USER_TASK_KEY] = isDoNewUserTask;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int lampCount = 0;
        public int LampCount
        {
            get
            {
                return lampCount;
            }
            set
            {
                lampCount = value;
                IsolatedStorageSettings.ApplicationSettings[LAMP_COUNT] = lampCount;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private bool isCommented = false;
        public bool IsCommented
        {
            get
            {
                return isCommented;
            }
            set
            {
                isCommented = value;
                IsolatedStorageSettings.ApplicationSettings[MARKET_COMMENT] = isCommented;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private int launchCount = 0;
        public int LaunchCount
        {
            get
            {
                return launchCount;
            }
            set
            {
                launchCount = value;
                IsolatedStorageSettings.ApplicationSettings[LAUNCH_COUNT] = launchCount;
            }
        }

        public Dictionary<int, StageData> stageDatas;
        public Dictionary<int, MapInfo> mapInfos;

        public void saveStageDatas()
        {
            IsolatedStorageSettings.ApplicationSettings[STAGE_DATA_KEY] = stageDatas;
            IsolatedStorageSettings.ApplicationSettings.Save();
        }

        /// <summary>
        /// 载入所有关卡
        /// </summary>
        public void InitAllStages()
        {
            mapInfos = new Dictionary<int, MapInfo>(StageCount);

            // 读取关卡信息
            for (int i = 1; i <= StageCount; i++)
            {
                MapInfo info = WordMapLoader.LoadMap(i);
                mapInfos.Add(i, info);
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAST_STAGE_KEY))
            {
                lastStage = (int)IsolatedStorageSettings.ApplicationSettings[LAST_STAGE_KEY];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAST_SCORE))
            {
                lastScore = (int)IsolatedStorageSettings.ApplicationSettings[LAST_SCORE];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAMP_COUNT))
            {
                lampCount = (int)IsolatedStorageSettings.ApplicationSettings[LAMP_COUNT];
            }
            else
            {
                LampCount = 2;
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(NEW_USER_TASK_KEY))
            {
                isDoNewUserTask = (bool)IsolatedStorageSettings.ApplicationSettings[NEW_USER_TASK_KEY];
            }


            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAUNCH_COUNT))
            {
                launchCount = (int)IsolatedStorageSettings.ApplicationSettings[LAUNCH_COUNT];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(MARKET_COMMENT))
            {
                isCommented = (bool)IsolatedStorageSettings.ApplicationSettings[MARKET_COMMENT];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(LAST_FULLSCREEN_DATE_KEY))
            {
                lastFullScreenDate = (string)IsolatedStorageSettings.ApplicationSettings[LAST_FULLSCREEN_DATE_KEY];
            }

            if (IsolatedStorageSettings.ApplicationSettings.Contains(STAGE_DATA_KEY))
            {
                stageDatas = IsolatedStorageSettings.ApplicationSettings[STAGE_DATA_KEY] as Dictionary<int, StageData>;
            }
            else
            {
                // 初始化
                stageDatas = new Dictionary<int, StageData>();
                stageDatas.Add(1, new StageData());

                //全部解锁初始化,VIP版本
                ////stageDatas = new Dictionary<int, StageData>();
                ////for (int i = 1; i <= StageCount; i++)
                ////{
                ////    stageDatas.Add(i, new StageData());
                ////}

                IsolatedStorageSettings.ApplicationSettings.Add(STAGE_DATA_KEY, stageDatas);
                IsolatedStorageSettings.ApplicationSettings.Save();
            }

        }


        internal StageData getCurrentStageData()
        {
            return stageDatas[LastStage];
        }

        internal void ClearCurrentStageDate()
        {
            stageDatas[LastStage] = new StageData();
        }
    }
}
